package vn.edu.saigontech.ncms.web.englishtest;

import java.util.Collection;

import vn.edu.saigontech.ncms.model.englishtest.Candidate;
import vn.edu.saigontech.ncms.model.enumeration.Gender;
import vn.edu.saigontech.ncms.service.CandidateService;
import vn.edu.saigontech.ncms.service.CandidateTypeService;
import vn.edu.saigontech.ncms.service.UserService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class CandidateAction extends BaseAction {
	
	private Long id;
	private Long[] ids;
	private String candidateCode;
	private String firstName;
	private String lastName;
	private String nickName;
	private String gender;
	private String dateOfBirth;
	private String phoneNumber;
	private String email;
	private String address;
	private String comment;
	private Long typeId;
	
	private String registeredDate;
	private Long consultantId;
	
	public String showAdd() {
		REQUEST().setAttribute("types", CandidateTypeService.INSTANCE.findAll());
		REQUEST().setAttribute("consultants", UserService.INSTANCE.findByRole("Consulting"));
		return SUCCESS;
	}

	public String add() {
		Candidate candidate = new Candidate();
		candidate.setCandidateCode(this.candidateCode);
		candidate.setFirstName(this.firstName);
		candidate.setLastName(this.lastName);
		candidate.setNickName(this.nickName);
		candidate.setGender(Gender.create(this.gender));
		candidate.setDateOfBirth(DateUtil.fromString(this.dateOfBirth, this.i18n("format.date")));
		candidate.setAddress(this.address);
		candidate.setPhoneNumber(this.phoneNumber);
		candidate.setEmail(this.email);
		candidate.setType(CandidateTypeService.INSTANCE.findById(this.typeId));
		candidate.setComment(this.comment);
		candidate.setRegisteredDate(DateUtil.fromString(this.registeredDate, this.i18n("format.date")));
		candidate.setConsultant(UserService.INSTANCE.findById(this.consultantId));
		
		candidate = CandidateService.INSTANCE.persist(candidate);
		this.id = candidate.getId();
		
		this.addLoggingDetail(BaseAction.ADD, "logging.candidate.add", candidate.getFullName());
		return SUCCESS;
	}
	
	public String info() {
		REQUEST().setAttribute("types", CandidateTypeService.INSTANCE.findAll());
		REQUEST().setAttribute("consultants", UserService.INSTANCE.findAllByRole("Consulting"));
		REQUEST().setAttribute("candidate", CandidateService.INSTANCE.findById(this.id));
		return SUCCESS;
	}
	
	public String edit() {
		Candidate candidate = CandidateService.INSTANCE.findById(this.id);
//		candidate.setCandidateCode(this.candidateCode);
		candidate.setFirstName(this.firstName);
		candidate.setLastName(this.lastName);
		candidate.setNickName(this.nickName);
		candidate.setGender(Gender.create(this.gender));
		candidate.setDateOfBirth(DateUtil.fromString(this.dateOfBirth, this.i18n("format.date")));
		candidate.setAddress(this.address);
		candidate.setPhoneNumber(this.phoneNumber);
		candidate.setEmail(this.email);
		candidate.setType(CandidateTypeService.INSTANCE.findById(this.typeId));
		candidate.setComment(this.comment);
		candidate.setRegisteredDate(DateUtil.fromString(this.registeredDate, this.i18n("format.date")));
		candidate.setConsultant(UserService.INSTANCE.findById(this.consultantId));
		
		CandidateService.INSTANCE.persist(candidate);
		
		this.addLoggingDetail(BaseAction.EDIT, "logging.candidate.edit", candidate.getFullName());
		return SUCCESS;
	}
	
	public String delete() {
		String fullName = CandidateService.INSTANCE.findById(this.id).getFullName();
		if(CandidateService.INSTANCE.deleteById(this.id)) {
			this.setKeyMessage("confirm.deleteSuccessfully", "candidate");
			this.addLoggingDetail(BaseAction.DELETE, "logging.candidate.delete", fullName);
		}
		else {
			this.setKeyMessage("confirm.deleteUnsuccessfully", "candidate");
		}
		return SUCCESS;
	}
	
	public String deleteSearch() {
		if(!Checker.isNull(this.ids)) {
			for (Long each : this.ids) {
				Candidate candidate = CandidateService.INSTANCE.findById(each);
				String name = candidate.getFullName();
				if(CandidateService.INSTANCE.deleteById(each)) {
					this.addLoggingDetail(BaseAction.DELETE, "logging.candidate.deleteSearch", name);
				}
			}
		}
		return SUCCESS;
	}
	
	public String cacheSearch() {
		return SUCCESS;
	}
	
//	public String merge() {
//		if(!Checker.isNull(this.ids)) {
//			StudentService.INSTANCE.merge(this.ids, this.ids[0]);
//		}
//		return SUCCESS;
//	}
	
	public String showCacheSearchResult() {
		Candidate candidate = (Candidate) SESSION().getAttribute("searchCandidate");
		if(Checker.isNull(candidate))
			candidate = new Candidate();
		Collection<Candidate> candidates = CandidateService.INSTANCE.find(candidate);
		SESSION().setAttribute("candidates", candidates);
		return SUCCESS;
	}
	
	public String showSearch() {
		REQUEST().setAttribute("types", CandidateTypeService.INSTANCE.findAll());
		REQUEST().setAttribute("consultants", UserService.INSTANCE.findAllByRole("Consulting"));
		return SUCCESS;
	}
	
	public String search() {
		Candidate candidate = new Candidate();
		candidate.setCandidateCode(this.candidateCode);
		candidate.setFirstName(this.firstName);
		candidate.setLastName(this.lastName);
		candidate.setNickName(this.nickName);
		candidate.setGender(Gender.create(this.gender));
		candidate.setDateOfBirth(DateUtil.fromString(this.dateOfBirth, this.i18n("format.date")));
		candidate.setAddress(this.address);
		candidate.setPhoneNumber(this.phoneNumber);
		candidate.setEmail(this.email);
		candidate.setType(CandidateTypeService.INSTANCE.findById(this.typeId));
		candidate.setComment(this.comment);
		candidate.setRegisteredDate(DateUtil.fromString(this.registeredDate, this.i18n("format.date")));
		candidate.setConsultant(UserService.INSTANCE.findById(this.consultantId));
		
		SESSION().setAttribute("searchCandidate", candidate);
		
		this.addLoggingDetail(BaseAction.SEARCH, "logging.candidate.search");
		return SUCCESS;
	}
	
//	public String showHistory() {
//		Student student = StudentService.INSTANCE.findById(this.id);
//		Level lastLevel = student.getLastLevel();
//		Level nextLevel = LevelService.INSTANCE.findNextLevel(lastLevel);
//		REQUEST().setAttribute("student", student);
//		REQUEST().setAttribute("nextLevel", nextLevel);
//		return SUCCESS;
//	}
	
//	public String requestByFirstNameAndLastName() {
//		Student searchStudent = new Student();
//		searchStudent.setFirstName(this.firstName);
//		searchStudent.setLastName(this.lastName);
//		Collection<Student> students = StudentService.INSTANCE.find(searchStudent);
//		List<SelectOption> optionList = new ArrayList<SelectOption>();
//		for(Student student : students) {
//			SelectOption selectOption = new SelectOption();
//			selectOption.setValue(String.valueOf(student.getId()));
//			selectOption.setCaption("Name: " + student.getFirstName() + " " + student.getLastName() + ", Gender: " + this.i18n(student.getGender().getKey()) + ", DOB: " + DateUtil.toString(student.getDateOfBirth(), this.i18n("format.date")));
//			optionList.add(selectOption);
//		}
//		try {
//			RESPONSE().getWriter().write(new JSONSerializer().serialize(optionList));
//		} catch (Exception ex) {
//			ex.printStackTrace();
//		}
//		return NONE;
//	}
	
	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public String getDateOfBirth() {
		return dateOfBirth;
	}

	public void setDateOfBirth(String dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Long[] getIds() {
		return ids;
	}

	public void setIds(Long[] ids) {
		this.ids = ids;
	}

	public Long getTypeId() {
		return typeId;
	}

	public void setTypeId(Long typeId) {
		this.typeId = typeId;
	}

	public String getRegisteredDate() {
		return registeredDate;
	}

	public void setRegisteredDate(String registeredDate) {
		this.registeredDate = registeredDate;
	}

	public Long getConsultantId() {
		return consultantId;
	}

	public void setConsultantId(Long consultantId) {
		this.consultantId = consultantId;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}
	public String getCandidateCode() {
		return candidateCode;
	}

	public void setCandidateCode(String candidateCode) {
		this.candidateCode = candidateCode;
	}
}